<?php

class CartModule extends fvModule {
	
	function __construct () {
	    $moduleName = strtolower(substr(__CLASS__, 0, -6));

	    parent::__construct(fvSite::$fvConfig->get("modules.{$moduleName}.smarty.template"), 
	                        fvSite::$fvConfig->get("modules.{$moduleName}.smarty.compile"), 
	                        fvSite::$Layoult);
	}

	function showIndex($params) {
	    $cook = new fvCookie();
	    $count = fvRequest::getInstance()->getRequestParameter("count");
    	if ($count){
    		foreach ($count as $k=>$v){
    			$cook->put("product", array($k => $v));
    		}
    	}
	    $vals = $cook->extract();
	    if ($vals["product"])
		foreach ($vals["product"] as $k=>$v){
    		$Products[$k] = ProductsManager::getInstance()->getByPk($k);
    		$Products[$k]["count"] = $v;
    	}
    	if ($Products)
    	foreach ($Products as $Product){
    		if ($Product->in_stock == 'yes' || $Product->in_stock == 'order'){
    			$ProductsCanOrder[] = $Product;
    		}
    		else{
    			$ProductsCanNotOrder[] = $Product;
    		}
    	}
	    $this->__assign("ProductsCanOrder", $ProductsCanOrder);
	    $this->__assign("ProductsCanNotOrder", $ProductsCanNotOrder);
	    return $this->__display("cart_index.tpl");
	}
	
	function showBlock($params) {
	    $cook = new fvCookie();
    	$vals = $cook->extract();
    	$count = 0;
    	$summ = 0;
    	if ($vals["product"])
    	foreach ($vals["product"] as $k=>$v){
    		$Product = ProductsManager::getInstance()->getByPk($k);
    		$count += $v;
    		$summ += ($Product->price - $Product->price*$Product->discount/100) * $v;
    	}
    	$this->__assign(array(
            'count' => $count,
        	'price' => number_format($summ, 2, ',', ' ')
        ));
        return $this->__display('cart_block.tpl');
	}
	
	function showOrder(){
		$cook = new fvCookie();
	    $count = fvRequest::getInstance()->getRequestParameter("count");
    	if ($count){
    		foreach ($count as $k=>$v){
    			$cook->put("product", array($k => $v));
    		}
    	}
	    $vals = $cook->extract();
	    if ($vals["product"])
		foreach ($vals["product"] as $k=>$v){
    		$Products[$k] = ProductsManager::getInstance()->getByPk($k);
    		$Products[$k]["count"] = $v;
    	}
    	if ($Products)
    	foreach ($Products as $Product){
    		if ($Product->in_stock == 'yes' || $Product->in_stock == 'order'){
    			$ProductsCanOrder[] = $Product;
    		}
    	}
    	if (fvSite::$fvSession->getCustomer())
    		$Customer = CustomerManager::getInstance()->getByPk(fvSite::$fvSession->getCustomer()->getPk());
    	else
    		$Customer = false;
    		
    	$this->__assign(array(
        	'payment_options' => array(
        		'cash' => 'Готівкою',
        		'nocash' => 'Безготівковий розрахунок', 
        	),
        	'delivery_options' => array(
        		'courier' => 'Кур"єром',
        		'own' => 'Самовивіз', 
        	),
        ));
		$this->__assign("LoggedUser", $Customer);
		$this->__assign('Error', fvSite::$fvSession->get('Error'));
	    $this->__assign("ProductsCanOrder", $ProductsCanOrder);
	    fvSite::$fvSession->set('Error', null);
	    return $this->__display("cart_order.tpl");
	}
	
	function showSuccess(){
		$Customer = CustomerManager::getInstance()->getByPk(fvSite::$fvSession->getCustomer()->getPk());
		$this->__assign("LoggedUser", $Customer);
	    $Order = OrderManager::getInstance()->getByPk(fvSite::$fvSession->get('order_id')); //
	    if ($Order["products"])
		foreach ($Order["products"] as $k=>$v){
    		$Products[$k] = ProductsManager::getInstance()->getByPk($v["product_id"]);
    		$Products[$k]["count"] = $v["count"];
    	}
    	fvSite::$fvSession->set('order_id', null);
    	$this->__assign("ProductsCanOrder", $Products);
    	$this->__assign("Order", $Order);
		return $this->__display("cart_success.tpl");
	}
}

?>
